package controllers;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;

import models.City;
import models.Province;
import models.TransferCompany;
import models.TransferPrice;
import play.mvc.Controller;
import service.TransPriceService;

import com.google.gson.Gson;

public class TransPrice extends Controller {
	

/**
 * 进入公有运价查询
 * 
 */
public static void publicindex(){
	
	//获取城市列表
	List<City> cityList = City.findAll();
	
	render(cityList);
}

/**
 * 查询公有运价
 */
public static void queryPublicTransPrice(String startCity,String endCity,Double weight){
	City start = City.findById(Long.valueOf(startCity));
	City end = City.findById(Long.valueOf(endCity));
	
	List<TransferPrice> priceList = TransferPrice.find("startCity.id=? and endCity.id=?", start.id,end.id).fetch();
	List<TransferCompany> companyList = TransferCompany.all().fetch();
	
	if(priceList!=null && companyList!=null){
		for(TransferPrice tp : priceList){
			//反查公司信息
			for(TransferCompany tc : companyList){
				List<TransferPrice> tempList = null;//tc.priceList;
				if(tempList!=null && tempList.contains(tp)){
					tp.company = tc;
					break;
				}
			}
		
			//计算运费总价
			tp.publicTotalPrice = TransPriceService.countTransferFee(tp.publicBasePrice, tp.publicRenewalPrice, weight);
		
		}
	}
	
	//获取城市列表
	List<City> cityList = City.findAll();
	render("@publicindex",cityList,priceList);
}
	
	/**
	 * Ajax 获取城市列表。
	 * @return
	 */
	public static String queryProvinces() {
		//获取城市列表
		List<Province> provinces = Province.findAll();
		List<Province> provincesForJson = new ArrayList<Province>();
		if(provinces != null) {
			for(Province province : provinces) {
				Province p = new Province();
				p.id = province.id;
				p.name = province.name;
				provincesForJson.add(p);
			}
		}
		Gson gson = new Gson();
		String result = gson.toJson(provincesForJson);
		return result;
	}
	
	/**
	 * Ajax获取城市列表。
	 * @return
	 */
	public static String queryCities(long provinceId) {
		//获取城市列表
		List<City> cities = City.find("province.id", provinceId).fetch();
		List<City> citiesForJson = new ArrayList<City>();
		if(cities != null) {
			for(City city : cities) {
				City c = new City();
				c.id = city.id;
				c.cityName = city.cityName;
				citiesForJson.add(c);
			}
		}
		Gson gson = new Gson();
		String result = gson.toJson(citiesForJson);
		return result;
	}
	
	/**
	 * 获取对应的价格。
	 * @param oriCityId
	 * @param destCityId
	 * @return
	 */
	public static String queryTransPrices(long oriCityId, long destCityId) {
		List<TransferPrice> transPrices = TransferPrice.find("startCity.id=? and endCity.id=?", oriCityId, destCityId).fetch();
		List<TransferPrice> transPricesForJson = new ArrayList<TransferPrice>();
		if(transPrices != null) {
			for(TransferPrice tp : transPrices) {
				TransferPrice t = new TransferPrice();
				t.id = tp.id;
				t.privateBasePrice = tp.privateBasePrice;
				t.privateBaseWeight = tp.privateBaseWeight;
				t.privateRenewalPrice = tp.privateRenewalPrice;
				t.company = tp.company;
				transPricesForJson .add(t);
			}
		}
		Gson gson = new Gson();
		String result = gson.toJson(transPricesForJson);
		return result;
	}
	
	/**
	 * 根据ID获取价格。
	 * @param id
	 * @return
	 */
	public static String queryTransPrice(long id) {
		TransferPrice tp = TransferPrice.findById(id);
		if(tp.endCity == null) {
			Hibernate.initialize(tp.endCity);
			Hibernate.initialize(tp.endCity.province);
		}
		String result = "{'transferPriceId':'"+tp.id+"','cityId':'"
			+tp.endCity.id+"','provinceId':'"+tp.endCity.province.id+"'}";
		return result;
	}
}
